using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Microsoft.Practices.WizardFramework;
using VSSDKAssist.Assets.CustomPages;
using VSSDKAssist.Recipes.Common.Converters;
using VSSDKAssist.Recipes.Common.Editors;
using Microsoft.VisualStudio.Shell;
using VSSDKAssist.Assets.Converters;
using VSSDKAssist.Recipes.Common.Helpers;

namespace VSSDKAssist.Recipes.CreateVSToolWindow
{
	/// <summary>
	/// Custom page for CreateVSToolWindow recipe
	/// </summary>
	public partial class CreateVSToolWindowCP1 : WizardPageBase
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="CreateVSToolWindowCP1"/> class.
		/// </summary>
		public CreateVSToolWindowCP1()
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="CreateVSToolWindowCP1"/> class.
		/// </summary>
		/// <param name="wizard">The <see cref="T:Microsoft.Practices.WizardFramework.WizardForm"/>that will host the new page.</param>
		public CreateVSToolWindowCP1(WizardForm wizard)
			: base(wizard)
		{
			InitializeComponent();
		}

		/// <summary>
		/// Gets or sets the destination folder.
		/// </summary>
		/// <value>The destination folder.</value>
		[RecipeArgument]
		public string DestinationFolder
		{
			get { return veDestinationFolder.Value.ToString(); }
			set
			{
				if(value != null)
				{
					veDestinationFolder.TextBox.Text = value;
				}
			}
		}

		/// <summary>
		/// Gets or sets the name of the tool window.
		/// </summary>
		/// <value>The name of the tool window.</value>
		[RecipeArgument]
		public string ToolWindowName
		{
			get { return veToolWindowName.Value.ToString(); }
			set
			{
				if(value != null)
				{
					veToolWindowName.TextBox.Text = value;
				}
			}
		}

		/// <summary>
		/// Gets or sets the name of the tool window control.
		/// </summary>
		/// <value>The name of the tool window control.</value>
		[RecipeArgument]
		public string ToolWindowControlName
		{
			get { return veToolWindowControlName.Value.ToString(); }
			set
			{
				if(value != null)
				{
					veToolWindowControlName.Text = value;
				}
			}
		}

		/// <summary>
		/// 	<see cref="P:Microsoft.WizardFramework.WizardPage.IsDataValid"/>
		/// Search for all the arguments that this Wizard gather
		/// If all of them are not null, then the Wizard can finish
		/// </summary>
		/// <value></value>
		public override bool IsDataValid
		{
			get
			{
				errorProvider.SetError(veToolWindowName, string.Empty);
				errorProvider.SetError(veToolWindowControlName, string.Empty);

				if(veToolWindowName.Value != null && veToolWindowControlName.Value != null)
				{
					if(veToolWindowName.Value.ToString().Equals(
						veToolWindowControlName.Value.ToString(), StringComparison.OrdinalIgnoreCase))
					{
						errorProvider.SetError(veToolWindowName, Properties.Resources.NamesShouldBeDifferent);
						errorProvider.SetError(veToolWindowControlName, Properties.Resources.NamesShouldBeDifferent);

						return false;
					}

					if(CustomPageHelper.ProjectItemExists(
						DictionaryService,
						"VSPackageProject",
						"DestinationFolder",
						string.Concat(veToolWindowName.Value.ToString(), Properties.Resources.CsExtension)))
					{
						return false;
					}

					if(CustomPageHelper.ProjectItemExists(
						DictionaryService,
						"VSPackageProject",
						"DestinationFolder",
						string.Concat(veToolWindowControlName.Value.ToString(), Properties.Resources.CsExtension)))
					{
						return false;
					}
				}

				return base.IsDataValid;
			}
		}

		/// <summary>
		/// Gets or sets the site of the control.
		/// </summary>
		/// <value></value>
		/// <returns>The <see cref="T:System.ComponentModel.ISite"></see> associated with the <see cref="T:System.Windows.Forms.Control"></see>, if any.</returns>
		/// <PermissionSet><IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence"/><IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/></PermissionSet>
		public override ISite Site
		{
			get
			{
				return base.Site;
			}
			set
			{
				base.Site = value;
				if(value != null)
				{
					this.veDestinationFolder.BeginInit();
					this.veDestinationFolder.ValueType = typeof(string);
					base.Site.Container.Add(veDestinationFolder);
					this.veDestinationFolder.ConverterInstance = new CSharpIdentifierConverter();
					this.veDestinationFolder.EndInit();

					this.veToolWindowName.BeginInit();
					this.veToolWindowName.ValueType = typeof(string);
					base.Site.Container.Add(veToolWindowName);
					this.veToolWindowName.ConverterInstance = new CSharpIdentifierConverter();
					this.veToolWindowName.EndInit();

					this.veToolWindowControlName.BeginInit();
					this.veToolWindowControlName.ValueType = typeof(string);
					base.Site.Container.Add(veToolWindowControlName);
					this.veToolWindowControlName.ConverterInstance = new CSharpIdentifierConverter();
					this.veToolWindowControlName.EndInit();
				}
			}
		}

		private void veToolWindowName_ValueChanged(object sender, System.ComponentModel.Design.ComponentChangedEventArgs e)
		{
			if(veToolWindowName.ConverterInstance != null)
			{
				errorProvider.SetError(veToolWindowName, string.Empty);

				if(veToolWindowName.Value != null)
				{
					if(CustomPageHelper.ProjectItemExists(
						DictionaryService,
						"VSPackageProject",
						"DestinationFolder",
						string.Concat(veToolWindowName.Value.ToString(), Properties.Resources.CsExtension)))
					{
						DictionaryService.SetValue("ToolWindowName", null);
						errorProvider.SetError(veToolWindowName, Properties.Resources.InvalidValue);
					}
					else
					{
						DictionaryService.SetValue("ToolWindowName", veToolWindowName.Value.ToString());
					}
				}
				else
				{
					DictionaryService.SetValue("ToolWindowName", null);
					errorProvider.SetError(veToolWindowName, Properties.Resources.InvalidValue);
				}
			}
			else
			{
				DictionaryService.SetValue("ToolWindowName", veToolWindowName.Value.ToString());
			}
		}

		private void veToolWindowControlName_ValueChanged(object sender, System.ComponentModel.Design.ComponentChangedEventArgs e)
		{
			if(veToolWindowControlName.ConverterInstance != null)
			{
				errorProvider.SetError(veToolWindowControlName, string.Empty);

				if(veToolWindowControlName.Value != null)
				{
					if(CustomPageHelper.ProjectItemExists(
						DictionaryService,
						"VSPackageProject",
						"DestinationFolder",
						string.Concat(veToolWindowControlName.Value.ToString(), Properties.Resources.CsExtension)))
					{
						DictionaryService.SetValue("ToolWindowControlName", null);
						errorProvider.SetError(veToolWindowControlName, Properties.Resources.InvalidValue);
					}
					else
					{
						DictionaryService.SetValue("ToolWindowControlName", veToolWindowControlName.Value.ToString());
					}
				}
				else
				{
					DictionaryService.SetValue("ToolWindowControlName", null);
					errorProvider.SetError(veToolWindowControlName, Properties.Resources.InvalidValue);
				}
			}
			else
			{
				DictionaryService.SetValue("ToolWindowControlName", veToolWindowControlName.Value.ToString());
			}
		}

		private void veDestinationFolder_ValueChanged(object sender, System.ComponentModel.Design.ComponentChangedEventArgs e)
		{
			if(veDestinationFolder.ConverterInstance != null)
			{
				errorProvider.SetError(veDestinationFolder, string.Empty);

				if(veDestinationFolder.Value != null)
				{
					DictionaryService.SetValue("DestinationFolder", veDestinationFolder.Value.ToString());
				}
				else
				{
					DictionaryService.SetValue("DestinationFolder", null);
					errorProvider.SetError(veDestinationFolder, Properties.Resources.InvalidValue);
				}
			}
			else
			{
				DictionaryService.SetValue("DestinationFolder", veDestinationFolder.Value.ToString());
			}
		}
	}
}